<?php namespace Phpcmf\Controllers\Admin;

class Config extends \Phpcmf\Common
{

    public function index() {

        $data = \Phpcmf\Service::M('app')->get_config(APP_DIR);

        $field = [
            'banner' => [
                'ismain' => 1,
                'fieldtype' => 'Files',
                'fieldname' => 'banner',
                'setting' => ['option' => ['ext' => 'jpg,gif,png,jpeg','count' => 10, 'size' => 10, 'input' => 1]]
            ]
        ];

        if (IS_AJAX_POST) {

            $post = \Phpcmf\Service::L('input')->post('data', true);
            \Phpcmf\Service::M('app')->save_config(APP_DIR, $post);

            // 附件归档
            if (SYS_ATTACHMENT_DB) {
                list($post, $return, $attach) = \Phpcmf\Service::L('form')->validation($post, null, $field);
                $attach && \Phpcmf\Service::M('Attachment')->handle($this->member['id'], \Phpcmf\Service::M()->dbprefix('site'), $attach);
            }

            $this->_json(1, dr_lang('操作成功'));
        }

        $page = intval(\Phpcmf\Service::L('input')->get('page'));

        \Phpcmf\Service::V()->assign([
            'page' => $page,
            'data' => $data,
            'form' => dr_form_hidden(['page' => $page]),
            'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
                [
                    '应用设置' => [APP_DIR.'/config/index', 'fa fa-cog'],
                    //'help' => [548],
                ]
            ),
            'banner_field' => dr_fieldform($field['banner'], $data['banner']),
        ]);
        \Phpcmf\Service::V()->display('config.html');

    }

}
